package com.rfid.client;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.rfid.listener.ResetListener;
import com.rfid.listener.SubmitListener;
import com.rfid.service.RFIDSaveInfoService;
import com.teatrace.model.TraceInfo;

@SuppressWarnings("serial")
public class SettingWindow extends JDialog {
	
	private JButton submit = new JButton("确定");
	private JButton reset = new JButton("取消");
	private JTextField numberText = new JTextField();
	private JTextField addressText = new JTextField();
	private JTextField recordTimeText = new JTextField();
	private JTextField descriptionText = new JTextField();
	
	private void initCmp() {

		JPanel buttonPanel = new JPanel();
		JPanel panel = new JPanel();
		JPanel logPanel = new JPanel();
		
		JPanel leftPanel = new JPanel();
		JPanel rightPanel = new JPanel();
		JPanel buttomPanel = new JPanel();
		JPanel contextPanel = new JPanel(new GridLayout(4,2,0,20));
		panel.setLayout(new BorderLayout(2,1));
		buttonPanel.add(submit);
		buttonPanel.add(reset);
		
		JLabel numberLable = new JLabel("number:",10);
		JLabel addressLable = new JLabel("address:");
		JLabel recordTimeLable = new JLabel("recordTime:");
		JLabel descriptionLable = new JLabel("description:");
		
		Properties p = new Properties();
		try {
			p.load(this.getClass().getClassLoader().getResourceAsStream("settingAttribute.properties"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		TraceInfo info = new TraceInfo();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		numberText.setText(p.getProperty("NUMBER"));
		info.setNumber(p.getProperty("NUMBER"));
		addressText.setText(p.getProperty("ADDRESS"));
		info.setAddress(p.getProperty("ADDRESS"));
		Date time = new Date();
		recordTimeText.setText(sdf.format(time));
		info.setRecordTime(time);
		descriptionText.setText(p.getProperty("DESCRIPTION"));
		info.setDescription(p.getProperty("DESCRIPTION"));
		RFIDSaveInfoService.setInfo(info);
		
		contextPanel.add(numberLable);
		contextPanel.add(numberText);
		contextPanel.add(addressLable);
		contextPanel.add(addressText);
		contextPanel.add(recordTimeLable);
		contextPanel.add(recordTimeText);
		contextPanel.add(descriptionLable);
		contextPanel.add(descriptionText);
		
		logPanel.setLayout(new GridLayout(1,1));
		logPanel.add(contextPanel);
		panel.add(buttonPanel,BorderLayout.NORTH);
		panel.add(logPanel,BorderLayout.CENTER);

		panel.add(leftPanel,BorderLayout.WEST);
		panel.add(rightPanel,BorderLayout.EAST);
		panel.add(buttomPanel,BorderLayout.SOUTH);
		this.add(panel);
		
	}
	
	private void initCmpListeners() {
		ActionListener submitListener = new SubmitListener(this);
		submit.addActionListener(submitListener);
		ActionListener resetListener = new ResetListener(this);
		reset.addActionListener(resetListener);
	}
	
	public SettingWindow(JFrame frame,boolean flag) {
		super(frame, flag);
		this.setTitle("写入信息设置");
		this.setSize(400, 300);
		Toolkit kit = Toolkit.getDefaultToolkit();
		Dimension screenSize = kit.getScreenSize();
		int screenWidth = (int) (screenSize.getWidth()/2);
		int screenHight = (int) (screenSize.getHeight()/2);
		int width = this.getWidth();
		int hight = this.getHeight();
		this.setLocation(screenWidth-width/2, screenHight-hight/2);
		
		this.initCmp();
		this.initCmpListeners();
	}
	public JTextField getNumberText() {
		return numberText;
	}
	public JTextField getAddressText() {
		return addressText;
	}
	public JTextField getRecordTimeText() {
		return recordTimeText;
	}
	public JTextField getDescriptionText() {
		return descriptionText;
	}
}
